package com.huawei.appgallery.foundation.ui.css;

import android.view.View;
import android.view.ViewGroup;
import com.huawei.appgallery.foundation.ui.css.adapter.RenderAdapterRegister;

public class CSSView {
    private CSSRule rule;
    private View view;

    CSSView(View view, CSSRule cSSRule) {
        this.view = view;
        this.rule = cSSRule;
    }

    public static CSSView wrap(View view, CSSRule cSSRule) {
        return new CSSView(view, cSSRule);
    }

    private CSSView renderImpl(View view, CSSRule cSSRule) {
        RenderAdapterRegister.get(view.getClass()).create(view).render(view, cSSRule);
        return this;
    }

    public CSSView render() {
        renderChildView(this.view);
        return this;
    }

    public CSSView render(boolean z) {
        if (z) {
            render();
        } else {
            renderImpl(this.view, this.rule);
        }
        return this;
    }

    private void renderChildView(View view) {
        String str = CSSSelectorMarker.get(view);
        if (str != null) {
            CSSRule rule = new CSSSelector(str).getRule(this.rule);
            if (rule != null) {
                Object obj;
                if (view instanceof RenderListener) {
                    if (!((RenderListener) view).onRenderReady(new CSSViewProxy(view, rule))) {
                        obj = null;
                        if (obj != null) {
                            renderImpl(view, rule);
                        }
                    }
                }
                int i = 1;
                if (obj != null) {
                    renderImpl(view, rule);
                }
            }
        }
        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            int childCount = viewGroup.getChildCount();
            for (i = 0; i < childCount; i++) {
                renderChildView(viewGroup.getChildAt(i));
            }
        }
    }

    public CSSRule getRule() {
        return this.rule;
    }

    public View getView() {
        return this.view;
    }
}
